package com.my.main;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.my.bean.ForWardBean;

public class PropertiesUtil {

	static String filePath;// = ClassLoader.getSystemResource("config.properties").getPath().toString();
//	System.out.println(GetValueByKey(filePath,"gangway.address"));
	public static void setFilePath(String filePath1){
		filePath = filePath1;
	}
	public static String GetValueByKey(String key) {
		if(null!=filePath){
			Properties pps = new Properties();
			try {
				InputStream in = new BufferedInputStream (new FileInputStream(filePath));  
				pps.load(in);
				String value = pps.getProperty(key);
//				System.out.println(key + " = " + value);
				return value;
			}catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	//根据Key读取Value
	public static String GetValueByKey(String filePath, String key) {
		Properties pps = new Properties();
		try {
			InputStream in = new BufferedInputStream (new FileInputStream(filePath));  
			pps.load(in);
			String value = pps.getProperty(key);
			System.out.println(key + " = " + value);
			return value;
		}catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static List<ForWardBean> getList(){
		List<ForWardBean> list = null;
		String[] ips = GetValueByKey("target.address").split(",");
		String[] targetPorts = GetValueByKey("target.port").split(",");
		String[] localPorts = GetValueByKey("local.forward.port").split(",");
		if(null!=ips&&ips.length>0
				&&null!=targetPorts&&targetPorts.length>0
				&&null!=localPorts&&localPorts.length>0&&ips.length==targetPorts.length&&localPorts.length==targetPorts.length){
			list = new ArrayList<ForWardBean>();
			for(int i=0;i<ips.length;i++){
				ForWardBean bean = new ForWardBean(ips[i],Integer.parseInt(targetPorts[i]),Integer.parseInt(localPorts[i]));
				list.add(bean);
			}
		}
		return list;
	}
	public static void main(String[] args) {
//		System.out.println(PropertiesUtil.class.getResource(""));
//		System.out.println(ClassLoader.getSystemResource("config.properties"));
//		String filePath = ClassLoader.getSystemResource("config.properties").getPath().toString();
		System.out.println(ClassLoader.getSystemResource("config.properties").getPath().toString());
	}
}
